<?php
namespace BWL\webshopBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @ORM\Entity
 * @ORM\Table(name="user")
 */
class User implements UserInterface
{

  /**
   * @ORM\Id
   * @ORM\Column(type="integer")
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  protected $id;


  /**
   * @ORM\Column(type="string", length=50)
   */
  protected $firstname;



  /**
   * @ORM\Column(type="string", length=50)
   */
  protected $lastname;


  /**
   *
   * @ORM\Column(type="string", length=255, unique=true)
   *
   * @var string username
   */
  protected $username;

  /**
   * @ORM\Column(type="string", length=255)
   *
   * @var string password
   */
  protected $password;

  /**
   * @ORM\Column(type="string", length=255)
   *
   * @var string salt
   */
  protected $salt;

  /**
   * @ORM\Column(type="string", length=255)
   *
   * @var
   */
  protected $email;

  /**
   * @ORM\ManyToMany(targetEntity="Role")
   * @ORM\JoinTable(name="user_role",
   *     joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
   *     inverseJoinColumns={@ORM\JoinColumn(name="role_name", referencedColumnName="name")}
   * )
   *
   * @var ArrayCollection $userRoles
   */
  protected $userRoles;

  /**
   * @ORM\OneToMany(targetEntity="Ordering", mappedBy="user")
   */
  protected $orderings;

  /**
   * @ORM\OneToMany(targetEntity="ShoppingCart", mappedBy="user")
   */
  protected $shoppingCart;

  public function getShoppingCart(){
    return $this->shoppingCart;
  }

  public function getOrderings(){
    $this->orderings;
  }

  public function setCreatedAt($createdAt) {
    $this->createdAt = $createdAt;
  }

  public function getCreatedAt() {
    return $this->createdAt;
  }

  /**
   * @param  $email
   */
  public function setEmail($email) {
    $this->email = $email;
  }

  /**
   * @return
   */
  public function getEmail() {
    return $this->email;
  }




  /**
   * Gets the username.
   *
   * @return string The username.
   */
  public function getUsername()
  {
    return $this->username;
  }

  /**
   * Sets the username.
   *
   * @param string $value The username.
   */
  public function setUsername($value)
  {
    $this->username = $value;
  }

  /**
   * Gets the user password.
   *
   * @return string The password.
   */
  public function getPassword()
  {
    return $this->password;
  }

  /**
   * Sets the user password.
   *
   * @param string $value The password.
   */
  public function setPassword($value)
  {
    $this->password = $value;
  }

  /**
   * Gets the user salt.
   *
   * @return string The salt.
   */
  public function getSalt()
  {
    return $this->salt;
  }

  /**
   * Sets the user salt.
   *
   * @param string $value The salt.
   */
  public function setSalt($value)
  {
    $this->salt = $value;
  }

  /**
   * Gets the user roles.
   *
   * @return ArrayCollection A Doctrine ArrayCollection
   */
  public function getUserRoles()
  {
    return $this->userRoles;
  }

  /**
   * Constructs a new instance of User
   */
  public function __construct()
  {
    $this->posts = new ArrayCollection();
    $this->userRoles = new ArrayCollection();
    $this->createdAt = new \DateTime();
  }

  /**
   * Gets an array of roles.
   *
   * @return array An array of Role objects
   */
  public function getRoles()
  {
    $roles = array();
    foreach ($this->userRoles as $role) {
      $roles[] = $role->getRole();
    }

    return $roles;
  }



  /**
   * Compares this user to another to determine if they are the same.
   *
   * @param UserInterface $user The user
   * @return boolean True if equal, false othwerwise.
   */
  public function equals(UserInterface $user)
  {
    return md5($this->getUsername()) == md5($user->getUsername());
  }

  /**
   * Removes sensitive data from the user.
   *
   * This is important if, at any given point, sensitive information like
   * the plain-text password is stored on this object.
   *
   * @return void
   */
  public function eraseCredentials() {
    // TODO: Implement eraseCredentials() method.
  }


  public function setFirstname($firstname) {
    $this->firstname = $firstname;
  }

  public function getFirstname() {
    return $this->firstname;
  }

  public function setId($id) {
    $this->id = $id;
  }

  public function getId() {
    return $this->id;
  }

  public function setLastname($lastname) {
    $this->lastname = $lastname;
  }

  public function getLastname() {
    return $this->lastname;
  }

}